package com.zhangzhibo.controller.provider;

import com.zhangzhibo.pojo.Provider;
import com.zhangzhibo.pojo.User;
import com.zhangzhibo.service.provider.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ProviderController {

    @Autowired
    @Qualifier("providerServiceImpl")
    private ProviderService providerService;

    //跳转供货商管理界面
    @RequestMapping("/user/providerDo/providerPage")
    public String providerPage(){
        return "providerlist";
    }

    @RequestMapping("/user/providerDo/getAllBillProvider")
    @ResponseBody
    public List<Provider> getAllBillProvider(){
        return providerService.getAllBillProvider();
    }

    //查询
    @RequestMapping("/user/providerDo/query")
    @ResponseBody
    public Map<String,Object> getProviderList(String queryProCode, String queryProName, int currentPageNo, int pageSize){
        Map<String, Object> map = new HashMap<>();
        int totalNum = providerService.getProviderCounts(queryProCode, queryProName,"",0);
        int totalPage = (totalNum/pageSize);
        if(totalPage < (float) totalNum/pageSize){
            totalPage = totalPage+1;
        }
        List<Provider> providerList = providerService.getProviderList(queryProCode, queryProName, currentPageNo, pageSize);
        map.put("totalNum",totalNum);
        map.put("totalPage",totalPage);
        map.put("providerList",providerList);
        return map;
    }

    //详情页
    @RequestMapping("/user/providerDo/check")
    @ResponseBody
    public Provider checkProvider(Integer id){
        Provider byId = providerService.findById(id);
        return byId;
    }

    //删除
    @RequestMapping("/user/providerDo/delete")
    public String deleteProvider(Integer id){
        providerService.deleteProvider(id);
        return "forward:/user/providerDo/query";
    }

    //供应商修改
    @RequestMapping("/user/providerDo/modifyPage")
    public String modifyPage(Model model,Integer id){
        //点击修改页面时，要将被修改的内容传给前端。
        Provider providerById = providerService.findById(id);
        model.addAttribute("provider",providerById);
        return "modifyprovider";
    }
    @RequestMapping("/user/providerDo/modify")
    public String modifyProvider(HttpServletRequest request, HttpServletResponse response, Integer id){
        HttpSession session = request.getSession();
        User userSession = (User)session.getAttribute("userSession");
        Provider provider = new Provider();
        provider.setId(Integer.parseInt(request.getParameter("proId")));
        provider.setModifyDate(new Date());
        provider.setModifyBy(userSession.getId());
        System.out.println(userSession.getUserRole());
        System.out.println(request.getParameter("proCode"));
        System.out.println(request.getParameter("managerProCode"));
        if(userSession.getUserRole()==1){
            System.out.println("administrators");
            provider.setProCode(request.getParameter("proCode"));
        }else{
            System.out.println("manager");
            provider.setProCode(request.getParameter("managerProCode"));
        }
        ModifyAndAddProvider(request,response,provider);
        providerService.modifyProvider(provider);
        return "redirect:/user/providerDo/providerPage";
    }
    //管理员修改时会调用 查供应商编码是否重复
    //非管理员进行修改时，供应商编码的修改框会被禁用，不存在失焦触发，所以不会被调用。
    @RequestMapping("/user/providerDo/checkProCode")
    @ResponseBody
    public int checkProCode(String ProCode, String OperationName, Integer id){
        return providerService.getProviderCounts(ProCode, "",OperationName,id);
    }



    //添加供应商
    @RequestMapping("/user/providerDo/addPage")
    public String addPage(){
        return "addprovider";
    }
    @RequestMapping("/user/providerDo/addProvider")
    public String addProvider(HttpServletRequest request, HttpServletResponse response){
        Provider provider = new Provider();
        provider.setCreationDate(new Date());
        User user =  (User)request.getSession().getAttribute("userSession");
        provider.setCreatedBy(user.getId());
        String str = request.getParameter("proCodeCity");
        //根据前端传来的城市首字母大写，获取所在城市最大值数字
        int num = providerService.checkProviderNumber(str)+1;
        String proCode = str+"_GYS_"+num;
        provider.setProCode(proCode);
        ModifyAndAddProvider(request,response,provider);
        providerService.addProvider(provider);
        return "redirect:/user/providerDo/providerPage";
    }


    public void ModifyAndAddProvider(HttpServletRequest request, HttpServletResponse response, Provider provider){
        provider.setProName(request.getParameter("proName"));
        provider.setProContact(request.getParameter("proContact"));
        provider.setProPhone(request.getParameter("proPhone"));
        provider.setProAddress(request.getParameter("proAddress"));
        provider.setProFax(request.getParameter("proFax"));
        provider.setProDesc(request.getParameter("proDesc"));
    }
}
